package com.and9.mchen.mcmusic.dao.impl;

import java.sql.Timestamp;
import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.dao.IBaseEntityDao;
import com.and9.mchen.mcmusic.dao.IStatisticsDao;
import com.and9.mchen.mcmusic.entity.Statistics;
import com.and9.mchen.mcmusic.exception.DBException;
import com.and9.mchen.mcmusic.exception.DaoException;
import com.and9.mchen.mcmusic.util.EntityHelper;
import com.and9.mchen.mcmusic.util.GEntityHelper;
import com.and9.mchen.mcmusic.util.QueryHelper;

public class StatisticsDaoImpl implements IStatisticsDao{

	private final static Log log = LogFactory.getLog(StatisticsDaoImpl.class);

	public static String TABLEMANE="statistics_table";
	
	public int createOrUpdateStatistics(Statistics statistics,
			String... propertyNames) throws DaoException {
		
		if(GEntityHelper.isEmpty(statistics)){
			return -1;
		}
		
		String sqlStr=null;
		Object[] propertyValues=null;
		
		if(GEntityHelper.isEmpty(statistics.getId()) || statistics.getId() <= 0){
			if(GEntityHelper.isEmpty(statistics.getUniqueId())){
				statistics.setUniqueId(EntityHelper.createUniqueId());
			}
			statistics.setCreateTime(new Timestamp(System.currentTimeMillis()));
			
			Object[] parentParams=QueryHelper.getFieldsVal(statistics.getParent(), IBaseEntityDao.BASEPROPERTYNAMES);
			Object[] statisticsParams=QueryHelper.getFieldsVal(statistics, IStatisticsDao.BASEPROPERTYNAMES);
			propertyValues=EntityHelper.groupArrays(parentParams, statisticsParams);
			
			propertyNames=EntityHelper.groupArrays(IBaseEntityDao.BASEPROPERTYNAMES, IStatisticsDao.BASEPROPERTYNAMES);
			sqlStr=QueryHelper.getInsertSQLByProperty(StatisticsDaoImpl.TABLEMANE, propertyNames);
			
		}else {
			
			if(statistics.getId().equals(1)){
				return 0;
			}
			
			if(GEntityHelper.isEmpty(propertyNames) || 
					EntityHelper.containBaseEntityPropertyName(propertyNames) == true){
				propertyNames=IStatisticsDao.BASEPROPERTYNAMES;
			}
			
			propertyValues=QueryHelper.getFieldsVal(statistics, propertyNames);
			sqlStr=QueryHelper.getUpdateSQL(StatisticsDaoImpl.TABLEMANE, statistics, "id="+statistics.getId(), propertyNames);
			
		}
		
		try {
			return QueryHelper.update(sqlStr, propertyValues);
		} catch (DBException e) {
			log.error("[function: createOrUpdateStatistics]", e);
			throw new DaoException("create or update statistics faliure");
		}
	
	}

	public int deleteStatisticsByStatisticsId(long id) throws DaoException {
		
		if(id <= 0 || id == 1){
			return -1;
		}
		
		String sqlStr=QueryHelper.getDeleteSQL(StatisticsDaoImpl.TABLEMANE, "id=?");
		
		try {
			return QueryHelper.update(sqlStr, id);
		} catch (DBException e) {
			log.error("[function: deleteStatisticsByStatisticsId]", e);
			throw new DaoException("delete statistics by statistics iId faliure");
		}

	}

	public Statistics getsStatisticsBySongId(long id) throws DaoException {
		
		if(id <=0){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} WHERE {0}.id={1}.statistics_id AND {1}.id=?", 
				StatisticsDaoImpl.TABLEMANE, SongDaoImpl.TABLEMANE);
		
		try {
			
			Statistics statistics=QueryHelper.read(Statistics.class, sqlStr, id);
			
			if(GEntityHelper.isEmpty(statistics) || statistics.getId().equals(1L)){
				return null;
			}
			
			return statistics;
		} catch (DBException e) {
			log.error("[function: getsStatisticsBySongId]", e);
			throw new DaoException("get statistics by song id faliure");
		}

	}

	public Statistics getStatisticsByAlbumId(long id) throws DaoException {
		
		if(id <=0){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} WHERE " +
				"{0}.id={1}.statistics_id AND {1}.id=?", 
					StatisticsDaoImpl.TABLEMANE, AlbumDaoImpl.TABLEMANE);
		
		try {
			
			Statistics statistics=QueryHelper.read(Statistics.class, sqlStr, id);
			
			if(GEntityHelper.isEmpty(statistics) || statistics.getId().equals(1L)){
				return null;
			}
			
			return statistics;
		} catch (DBException e) {
			log.error("[function: getStatisticsByAlbumId]", e);
			throw new DaoException("get statistics by album id faliure");
		}
	}

	public Statistics getStatisticsBySingerId(long id) throws DaoException {

		if(id <= 1L){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} WHERE {0}.id={1}.statistics_id AND {1}.id=?", 
				StatisticsDaoImpl.TABLEMANE, SingerDaoImpl.TABLEMANE);
		
		try {
			
			Statistics statistics=QueryHelper.read(Statistics.class, sqlStr, id);
			
			if(GEntityHelper.isEmpty(statistics) || statistics.getId().equals(1L)){
				return null;
			}
			
			return statistics;
		} catch (DBException e) {
			log.error("[function: getStatisticsBySingerId]", e);
			throw new DaoException("get statistics by singer id faliure");
		}
		
	}

	public Statistics getStatisticsByStatisticsId(long id) throws DaoException {
		
		if(id <= 0){
			return null;
		}
		
		String sqlsStr=QueryHelper.getSelectSQL(StatisticsDaoImpl.TABLEMANE, "id=?");
		
		try {
			return QueryHelper.read(Statistics.class, sqlsStr, id);
		} catch (DBException e) {
			log.error("[function: getStatisticsByStatisticsId]", e);
			throw new DaoException("get statistics by statistics id faliure");
		}
	}

	public Statistics getStatisticsByStatisticsUniqueId(String uniqueId)
			throws DaoException {
		
		if(GEntityHelper.isEmpty(uniqueId)){
			return null;
		}
		
		String sqlStr=QueryHelper.getSelectSQL(StatisticsDaoImpl.TABLEMANE, "uniqueId=?");
		
		try {
			return QueryHelper.read(Statistics.class, sqlStr, uniqueId);
		} catch (DBException e) {
			log.error("[function: getStatisticsByStatisticsUniqueId]", e);
			throw new DaoException("get statistics by statistics uniqueId faliure");
		}
	
	}

}
